﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;
using System.Data.Common;

namespace RentalSystem.Data.EF.Dao
{
    public class EFMovieKindsDao : IMovieKindsDao
    {
        #region Queries

        private readonly string GetByIdQuery = "SELECT VALUE mk FROM MovieKinds AS mk WHERE mk.MovieKindID = @Id;";
        private readonly string GetByNameQuery = "SELECT VALUE mk FROM MovieKinds AS mk WHERE mk.MovieKindName = @Name;";
        private readonly string GetMMKQuery = "SELECT VALUE mmk FROM MovieMovieKinds AS mmk WHERE mmk.MovieMovieID = @Id;";
        private readonly string ListQuery;

        #endregion

        private EFManager<MovieKinds> manager;

        public EFMovieKindsDao(RentalDatabaseEntities repository)
        {
            ListQuery = "SELECT VALUE mk FROM MovieKinds AS mk;";

            this.manager = new EFManager<MovieKinds>(repository);
        }

        #region IDao1Key

        public MovieKinds Add(MovieKinds entity)
        {
            manager.Database.AddToMovieKinds(entity);
            manager.Save();
            return entity;
        }

        public MovieKinds Get(int id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Id", id)
                };

            var kinds = manager.SQLQueryToEntity(GetByIdQuery, parameters);

            foreach (MovieKinds kind in kinds)
            {
                return kind;
            }

            return null;
        }

        public void Delete(int id)
        {
            MovieKinds kind = Get(id);

            if (kind != null)
            {
                foreach(MovieMovieKinds mmk in kind.MovieMovieKinds)
                {
                    manager.Database.MovieMovieKinds.DeleteObject(mmk);
                }

                manager.Database.MovieKinds.DeleteObject(kind);
                manager.Save();
            }
        }

        public void Delete(MovieKinds entity)
        {
            throw new NotImplementedException();
        }

        public void Update(MovieKinds entity, uint fields)
        {
            MovieKinds kind = Get(entity.MovieKindID);

            if (kind != null)
            {
                if ((fields & (uint)MovieKindFields.Description) == (uint)MovieKindFields.Description)
                    kind.MovieKindDescription = entity.MovieKindDescription;
                if ((fields & (uint)MovieKindFields.Name) == (uint)MovieKindFields.Name)
                    kind.MovieKindName = entity.MovieKindName;
                if ((fields & (uint)MovieKindFields.Shortcut) == (uint)MovieKindFields.Shortcut)
                    kind.MovieKindShortcut = entity.MovieKindShortcut;

                manager.Save();
            }
        }

        public List<MovieKinds> List()
        {
            List<MovieKinds> list = new List<MovieKinds>();

            var kinds = manager.SQLQueryToEntity(ListQuery);

            foreach (MovieKinds kind in kinds)
            {
                list.Add(kind);
            }

            return list;
        }

        #endregion
        
        public MovieKinds GetByName(string name)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Name", name)
                };

            var kinds = manager.SQLQueryToEntity(GetByNameQuery, parameters);

            foreach (MovieKinds kind in kinds)
            {
                return kind;
            }

            return null;
        }
    }
}
